package com.hmo.appshop;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.leanback.widget.Presenter;
import androidx.leanback.widget.VerticalGridView;

public class GridItemPresenter extends Presenter implements View.OnClickListener {

    Context context;
    VerticalGridView gridView;


    public GridItemPresenter(Context context) {
        this.context=context;
    }

    public GridItemPresenter(Context context, VerticalGridView gridView) {
        this.context = context;
        this.gridView = gridView;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Object item) {
        final AppBean appBean = (AppBean) item;
        final ImageButton logo = viewHolder.view.findViewById(R.id.app_logo);
        TextView name = viewHolder.view.findViewById(R.id.app_name);
        name.setText(appBean.getName());
        logo.setBackgroundColor(Color.TRANSPARENT);
        logo.setImageBitmap(appBean.getLogo());

        ConstraintLayout view = viewHolder.view.findViewById(R.id.app_item);
        view.setTag(appBean.getId());
        view.setOnClickListener(this);

        viewHolder.view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
                    return true;
                } else if (i == KeyEvent.KEYCODE_DPAD_UP) {
                    if (appBean.getId() < 5) {
                        gridView.setFocusable(false);
                        MainActivity.setRadioFocues();
                    }
                }else if (i== KeyEvent.KEYCODE_DPAD_CENTER){
                    Intent intent = new Intent(context, DetailsActivity.class);
                    intent.putExtra("id",appBean.getId());
                    intent.putExtra("type",appBean.getType());
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
                return false;
            }
        });
    }

    @Override
    public void onUnbindViewHolder(ViewHolder viewHolder) {

    }


    @Override
    public void onClick(View view) {
        Log.d("GridItem:","onclick");
        Object tag = view.getTag();
        Intent intent = new Intent(context, DetailsActivity.class);
        intent.putExtra("id",String.valueOf(tag));
        context.startActivity(intent);
    }
}
